<template>
  <div class="joblevelMan-container">
    <div class="joblevelMan-header">
      <el-input v-model="jl.name" size="small" placeholder="请添加职称等级..." prefix-icon="el-icon-plus" style="width: 300px"></el-input>
      <el-select size="small" v-model="jl.titleLevel" placeholder="职称等级">
        <el-option v-for="item in titleLevels" :key="item" :label="item" :value="item"> </el-option>
      </el-select>
      <el-button size="small" type="primary" icon="el-icon-plus" @click="addJobLevel">添加</el-button>
      <el-button type="danger" size="small" :disabled="this.multipleSelection.length == 0" @click="deleteMany">批量删除</el-button>
    </div>
    <div class="joblevelMan-body" style="margin-top: 10px">
      <el-table :data="jls" style="width: 70%" size="small" border @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="=55"></el-table-column>
        <el-table-column prop="id" label="编号" width="55"> </el-table-column>
        <el-table-column prop="name" label="职称名称" width="150"> </el-table-column>
        <el-table-column prop="titleLevel" label="职称等级" width="150"> </el-table-column>
        <el-table-column prop="createDate" label="创建日期" width="150"> </el-table-column>
        <el-table-column prop="enabled" label="是否启用" width="150">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.enabled" type="success">已启用</el-tag>
            <el-tag v-else type="danger">未启用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作"
          ><template slot-scope="scope">
            <el-button size="small" type="primary" @click="showEditView(scope.row)">编辑</el-button>
            <el-button size="small" type="danger" @click="deleteHandle(scope.row)">删除</el-button>
          </template></el-table-column
        >
      </el-table>
    </div>
    <el-dialog title="编辑职称" :visible.sync="dialogVisible" width="30%">
      <table>
        <tr>
          <td><el-tag>职称名称</el-tag></td>
          <td>
            <el-input v-model="updateJl.name" size="small" style="margin-left: 6px"></el-input>
          </td>
        </tr>
        <tr>
          <td><el-tag>职称等级</el-tag></td>
          <td>
            <el-select size="small" v-model="updateJl.titleLevel" placeholder="职称等级" style="margin-left: 6px; margin-right: 6px">
              <el-option v-for="item in titleLevels" :key="item" :label="item" :value="item"> </el-option>
            </el-select>
          </td>
        </tr>
        <tr>
          <td><el-tag>是否启用</el-tag></td>
          <td>
            <el-switch style="margin-left: 6px" v-model="updateJl.enabled" active-color="#13ce66" inactive-color="#ff4949" active-text="已启用" inactive-text="未启用"> </el-switch>
          </td>
        </tr>
      </table>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogVisible = false">取 消</el-button>
        <el-button size="small" type="primary" @click="doUpdate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'JoblevelMana',
  created() {
    this.initJls()
  },
  data() {
    return {
      //更新
      updateJl: {
        name: '',
        titleLevel: '',
        enabled: false,
      },
      //添加
      jl: {
        name: '',
        titleLevel: '',
      },
      titleLevels: ['正高级', '副高级', '中级', '初级', '员级'],
      // table数据源
      jls: [],
      multipleSelection: [],
      dialogVisible: false,
    }
  },
  methods: {
    async initJls() {
      const data = await this.getRequest('/system/basic/joblevel/')
      this.jls = data
    },
    async addJobLevel() {
      if (this.jl.name && this.jl.titleLevel) {
        const data = await this.postRequest('/system/basic/joblevel/', this.jl)
        if (data) {
          //刷新
          this.initJls()
          this.jl.name = ''
          this.jl.titleLevel = ''
        }
      } else {
        this.$message('职称和等级不能为空')
      }
    },
    handleSelectionChange(selections) {
      this.multipleSelection = selections
    },
    async deleteMany() {
      this.$confirm('此操作将删[' + this.multipleSelection.length + ']条职称, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          let queryStr = 'ids='
          this.multipleSelection.forEach((item, index) => {
            // console.log(item.id)
            queryStr += index == 0 ? item.id : '&ids=' + item.id
          })
          // console.log(queryStr)
          this.deleteRequest('/system/basic/joblevel/?' + queryStr).then((data) => {
            if (data) {
              this.initJls()
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          })
        })
    },
    showEditView(jl) {
      Object.assign(this.updateJl, jl)
      this.updateJl.createDate = ''
      this.dialogVisible = true
    },
    deleteHandle(jl) {
      this.$confirm('此操作将删 [ ' + jl.name + ' ] 职称, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.deleteRequest('/system/basic/joblevel/' + jl.id).then((resp) => {
            if (resp) {
              this.initJls()
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          })
        })
    },
    async doUpdate() {
      const data = await this.putRequest('/system/basic/joblevel/', this.updateJl)
      if (data) {
        this.initJls()
        this.dialogVisible = false
      }
    },
  },
}
</script>

<style lang="less" scoped>
.joblevelMan-header {
  .el-input {
    width: 300px;
  }
  .el-select {
    margin: 0 6px;
  }
  .el-button {
    margin-left: 8px;
  }
}
</style>
